package thinkingInJava.generics;

//: generics/Erased.java
//{CompileTimeError} (Won't compile)
import static thinkingInJava.YTool.YPrint.*;

public class Erased<T> {
	private final int SIZE = 100;
	private T obj;
	//private Class<T> typ;	// 如果只需要知道T的类型,用 typ.isInstance 就行了.
	public void set(T o){obj = o;}
	//public static void f(Object arg) {
	public void f(Object arg) {
		if( obj.getClass().isInstance(arg) )	// 估计Class.isInstance 是静态的，所以没被擦除,可以在泛型范围内使用
			println(arg.getClass().getName() + " isinstance " + obj.getClass().getName());
		else println(arg.getClass().getName() + " notinstance " + obj.getClass().getName());
	// 下面这些都是动态类型检测,在泛型内部全都不能用
	 //if(arg instanceof T) {}          // Error: Cannot perform instanceof check against type parameter T. Use its erasure Object instead since further generic type information will be erased at runtime
	 //T var = new T();                 // Error: Cannot make a static reference to the non-static type T
	 //T[] array = new T[SIZE];         // 泛型数组 Error: Cannot make a static reference to the non-static type T
	 //T[] array = (T)new Object[SIZE]; // 泛型数组 Error:
	}
	
	public static void main(){
		String s = "1233";
		Erased<String> ee = new Erased<String>();
		ee.set(s);
		
		int i = 3;
		ee.f("13");
		ee.f(i);
		//ee.f(null);
	}
} ///:~
